#include "Info.h"

#define KEY_DOWN(vk_code) ((GetAsyncKeyState(vk_code) & 0x8000)? 1: 0)


CInfo::CInfo(void)
{

}

CInfo::CInfo(CResourceManager* rm)
{
	_marioText = rm->getSprite(CResourceManager::MARIOTEXT);
	_coinInfo = rm->getSprite(CResourceManager::COININFO);
	_gameOver = rm->getSprite(CResourceManager::GAMEOVER);
	_score = rm->getSprite(CResourceManager::SCORE);
	_chooser = rm->getSprite(CResourceManager::MARIOMOVERIGHT1);

	_MenuTop = 370;
	_MenuLeft = 575;

	_menuIndex = 0;

	delayTime = 0;
}


CInfo::~CInfo(void)
{
}

void CInfo::Render(int life, int coin, int score)
{
	_marioText->Render(20, 10);
	_coinInfo->Render(650, 10);
	int w,h;
	_marioText->getSize(w,h);
	RenderNumber(life,20 + w, 10);
	_coinInfo->getSize(w,h);
	RenderNumber(coin,650 + w, 10);
	RenderNumber(score,25, 25);
}

void CInfo::RenderGameOver(int x, int y)
{
	_gameOver->Render(x,y);
}

void CInfo::RenderNumber(int Number, float x, float y)
{
	if (Number == 0)
	{
		_score->RenderIndex(0, x + 2, y);
		return;
	}

	int list[5];
	int tmp = Number;
	int extra;
	int index = 0;
	while (tmp > 0)
	{
		list[index++] = tmp % 10;
		tmp = tmp / 10;
	}

	for (int i = index - 1; i >= 0; i--)
			_score->RenderIndex(list[i], x + (index - i) * 15 + 2, y);
}

void CInfo::RenderDynamicNumber(int Number, float x, float y)
{
	Vector v = CScreen::getInstance()->getPosition();
	RenderNumber(Number, x - v.getX(), y - v.getY());
}

void CInfo::KeyProcess( int &Action)
{
	if (delayTime > 0)
	{
		delayTime--;
		return;
	}

	Action = -1;
	if (KEY_DOWN(VK_UP))
	{
			_menuIndex--;
			if (_menuIndex < 0)
				_menuIndex = 0;
			delayTime = 5;
			CMarioSound::getInstance()->Play(CMarioSound::SOUND_MENUCHANGE,false);
	}
	else
		if (KEY_DOWN(VK_DOWN))
		{
			_menuIndex++;
			if (_menuIndex > 5)
				_menuIndex = 5;
			delayTime = 5;
			CMarioSound::getInstance()->Play(CMarioSound::SOUND_MENUCHANGE,false);
		}
		else if (KEY_DOWN(VK_RETURN))
		{
			Action = _menuIndex;
			CMarioSound::getInstance()->Play(CMarioSound::SOUND_MENUCHANGE,false);
			delayTime = 5;
		}
		else if (KEY_DOWN(VK_ESCAPE))
		{
			if (_isViewHelp)
				_isViewHelp = false;
		}
	
}

void CInfo::RenderMenu()
{
	if (!_isViewHelp)
		_chooser->Render(_MenuLeft,_MenuTop + _menuIndex * 37);
}


